package org.example.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DynamicTableExample {
    public static void main(String[] args) {
        // 创建文档对象
        Document document = new Document();
        try {
            // 创建 PDF 文件输出流
            PdfWriter.getInstance(document, new FileOutputStream("DynamicTable.pdf"));
            document.open();

            // 定义表格的列数
            PdfPTable table = new PdfPTable(3); // 3列
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font0 = new Font(bf, 13);
            Font font1 = new Font(bf, 12);
            Font font3 = new Font(bf, 8);
            table.setWidthPercentage(100); // 设置表格宽度为 100%

            // 添加表头
            addHeader(table, font0);
            
            // 添加动态数据
            addDataRows(table);

            // 将表格添加到文档中
            document.add(table);

            // 关闭文档
            document.close();
        } catch (DocumentException | java.io.IOException e) {
            e.printStackTrace();
        }
    }

    // 添加表头的方法
    private static void addHeader(PdfPTable table, Font font0) {
        // 创建表头单元格
        PdfPCell cell = new PdfPCell();
        cell.setBorderWidth(2);

        // 设置表头内容
        cell.setPhrase(new Phrase("ID"));
        table.addCell(cell);
        cell.setPhrase(new Paragraph("名称", font0));
        table.addCell(cell);
        cell.setPhrase(new Phrase("Date"));
        table.addCell(cell);
    }

    // 添加数据行的方法
    private static void addDataRows(PdfPTable table) {
        // 创建一个动态数据列表（模拟数据）
        List<DataModel> dataList = generateDynamicData();

        // 遍历数据列表并填充表格
        for (DataModel data : dataList) {
            // 创建单元格
            PdfPCell cell = new PdfPCell();
            cell.setBorderWidth(1);

            // 填充数据
            cell.setPhrase(new Phrase(String.valueOf(data.getId())));
            table.addCell(cell);

            cell.setPhrase(new Phrase(data.getName()));
            table.addCell(cell);

            cell.setPhrase(new Phrase(data.getDate().toString()));
            table.addCell(cell);
        }
    }

    // 生成动态数据的方法
    private static List<DataModel> generateDynamicData() {
        List<DataModel> dataList = new ArrayList<>();
        // 模拟动态数据（实际应用中可以替换为数据库查询或其他数据源）
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        dataList.add(new DataModel(1, "Alice", new Date()));
        dataList.add(new DataModel(2, "Bob", new Date()));
        dataList.add(new DataModel(3, "Charlie", new Date()));
        dataList.add(new DataModel(4, "David", new Date()));
        return dataList;
    }

    // 数据模型类（模拟数据结构）
    private static class DataModel {
        private int id;
        private String name;
        private Date date;

        public DataModel(int id, String name, java.util.Date date) {
            this.id = id;
            this.name = name;
            this.date = date;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public Date getDate() {
            return date;
        }
    }
}
